/*
 * @Description:扩展类方法
 * @Author: r_yuesheng
 * @Date: 2021-06-02 08:48:24
 * @LastEditTime: 2022-12-23 15:34:44
 * @LastEditors: r_yuesheng
 */
import RouterData from '@/router/index.js';
// 路由数据
export function routerMenuData() {
  return RouterData.filter(item => item.name === '首页')[0].children;
}

export function initApiUrl() {
  const apiBaseUrl =
    process.env.NODE_ENV === 'production' ? window.apiProdUrl : window.apiDevUrl;
  sessionStorage.setItem('apiUrl', JSON.stringify(apiBaseUrl));
}


export function getGuid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    const r = (Math.random() * 16) | 0;
    const v = c === 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

/**
 * 获取当前时间，小时：分钟
 */
export function getTime() {
  const date = new Date();
  let minute = date.getMinutes();
  let hour = date.getHours();
  if (minute < 10) {
    minute = `0${minute}`;
  }
  if (hour < 10) {
    hour = `0${hour}`;
  }
  return `${hour}:${minute}`;
}

// 数据容量转换
export function resolveSize(bytes) {
  if (bytes === 0) return '0 B';
  const k = 1000;
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return `${(bytes / Math.pow(k, i)).toPrecision(3)} ${sizes[i]}`;
}

export function resolveApiUrl(name) {
  const listApiBaseurl = sessionStorage.getItem('apiUrl');
  if (listApiBaseurl && listApiBaseurl !== 'undefined') {
    return JSON.parse(listApiBaseurl)[name];
  }
  return '';
}

export function resolveTime(item) {
  if (item) {
    return item.replace('T', ' ').substring(0, 19);
  }
  return '';
}

// 深拷贝
export function deepCopy(params) {
  return JSON.parse(JSON.stringify(params));
}
